#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX 40000

typedef struct{
    char Id[15];
    int winTotal;
    int TestRoom;
    int winRoom;
    int Score;
} Student;

Student student[MAX];

typedef struct{
    char Id[15];
    int Score;
    int win;
} LocalStudent;

LocalStudent l_student[MAX];

int totalPos=0;

int cmp_local_student(const void *a,const void *b);
int getMid(int left,int right);
void _sort(int left, int right);
int rightJudge(Student a,Student b);
int leftJudge(Student a,Student b);

int main(){
    int n,k,end, i, j;
    scanf("%d",&n);
    for(i=0; i<n; i++){
        scanf("%d",&k);
        for(j=0; j<k; j++){
            scanf("%s %d",l_student[j].Id,&l_student[j].Score);
        }
        qsort(l_student,k,sizeof(LocalStudent),cmp_local_student);
        l_student[0].win=1;
        for(j=1; j<k; j++){
            if(l_student[j].Score==l_student[j-1].Score){
                l_student[j].win=l_student[j-1].win;
            }else{
                l_student[j].win=j+1;
            }
        }
        end=totalPos+k;
        for(j=0; totalPos<end; totalPos++,j++){
            strcpy(student[totalPos].Id,l_student[j].Id);
            student[totalPos].TestRoom=i+1;
            student[totalPos].winRoom=l_student[j].win;
            student[totalPos].Score=l_student[j].Score;
        }
    }
    _sort(0,totalPos-1);
    student[0].winTotal=1;
    for(i=1; i<totalPos; i++){
        if(student[i].Score==student[i-1].Score){
            student[i].winTotal=student[i-1].winTotal;
        }else{
            student[i].winTotal=i+1;
        }
    }
    printf("%d\n",totalPos);
    for(i=0; i<totalPos; i++){
        printf("%s %d %d %d\n",student[i].Id,student[i].winTotal,student[i].TestRoom,student[i].winRoom);
    }
    return 0;
}

int cmp_local_student(const void *a,const void *b){
    int res=((LocalStudent*)a)->Score - ((LocalStudent*)b)->Score;
    if(res == 0){
        res=strcmp(((LocalStudent*)a)->Id, ((LocalStudent*)b)->Id);
        if(res>0){
            return 1;
        }else{
            return 0;
        }
    }else if(res<0){
        return 1;
    }else{
        return 0;
    }
}

void _sort(int left, int right){
    if(left<right){
        int middle=getMid(left,right);
        _sort(left,middle);
        _sort(middle+1,right);
    }
}

int getMid(int left,int right){
    Student temp=student[left];
    while(left<right){
        while(left<right && rightJudge(student[right],temp)){
            right--;
        }
        student[left]=student[right];
        while(left<right && leftJudge(student[left],temp)){
            left++;
        }
        student[right]=student[left];
    }
    student[left]=temp;
    return left;
}

int rightJudge(Student a,Student b){
    int res=b.Score-a.Score;
    if(res==0){
        res=strcmp(b.Id, a.Id);
        if(res>0){
            return 0;
        }else{
            return 1;
        }
    }else if(res>0){
        return 1;
    }else{
        return 0;
    }
}

int leftJudge(Student a,Student b){
    int res=a.Score-b.Score;
    if(res==0){
        res=strcmp(a.Id, b.Id);
        if(res>0){
            return 0;
        }else{
            return 1;
        }
    }else if(res>0){
        return 1;
    }else{
        return 0;
    }
}

